package com.dx.wlmq.controller;

import com.alibaba.fastjson.JSON;
import com.dx.wlmq.domain.SysUser;
import org.springframework.http.MediaType;
import org.springframework.http.converter.json.Jackson2ObjectMapperFactoryBean;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;


import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;

/**
 * @ClassName DemoController
 * @Description TODO:
 * @Author 12994
 * @Date 2024/10/19 19:00
 * @Version 1.0
 **/
// 提示该类是一个controller
@Controller
public class DemoController {

    // 定义: 监听路由, 允许的请求方法, 编码格式等
    @RequestMapping(value = "/index", method = RequestMethod.GET, produces = "application/json; charset=utf-8;")
    // 提示这个接口直接通过json数据格式响应数据 =? 真的吗
    @ResponseBody
    public String index(HttpServletResponse response) {
        response.setContentType("application/json; charset=utf-8;");

        System.out.println("访问了 '/index' 路径下的一个Controller");
        return "访问了 '/index' 路径下的一个Controller";
    }

    @RequestMapping(value = "/user", method = RequestMethod.GET, produces = "application/json; charset=utf-8;")
    @ResponseBody
    // 声明需求一个参数userId
    public String user(Integer userId, HttpServletResponse response) {
        response.setContentType("application/json; charset=utf-8;");

        System.out.printf("当前传入的userId是%d", userId);
        return JSON.toJSONString("{userId:" + userId + "}");
    }

    @RequestMapping(value = "/user2", method = RequestMethod.GET, produces = "application/json; charset=utf-8;")
    @ResponseBody
    // 声明需求多个参数 => 可以设置键名,是否必须等选项
    public String user2(@RequestParam(value = "userName", required = true) String uName,
                        @RequestParam(value = "userPassword", required = false) String uPswd
            , HttpServletResponse response) {
        response.setContentType("application/json; charset=utf-8;");

        System.out.printf("当前传入的userName是%s, userPassword是%s", uName, uPswd);
        return JSON.toJSONString(String.format("{userName:%s,userPassword:%s}", uName, uPswd));
    }

    // 在RequestMapping中 value若传入dict like的多个value, 则可以同时监听多个url
    @RequestMapping(value = {"/user3", "/adduser"}, method = RequestMethod.POST, produces = "application/json; charset=utf-8;", consumes = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    // 声明需求多个参数 => 可以设置键名,是否必须等选项
    // 声明需求一个实体类 注意需要jackson包的依赖
    public String user3(@RequestBody() SysUser user, HttpServletResponse response) {
        response.setContentType("application/json; charset=utf-8;");
        SysUser sysUser = null;
        System.out.println(user.getUserName());
        ArrayList<SysUser> sysUserArrayList = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            sysUser = new SysUser();
            sysUser.setUserId(user.getUserId());
            sysUser.setUserName(user.getUserName());
            sysUser.setUserPassword(user.getUserPassword());
            sysUser.setPhone(user.getPhone());
            sysUserArrayList.add(sysUser);
        }
        return JSON.toJSONString(sysUserArrayList);
    }
}
